﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using HiComponents.IEvolution;
using ImageCom.LeadtoolEx;

namespace ImageCom.ImageEn
{
   public static class ImageEnExt
    {
       static  ImageCropPool ieImagePool = new ImageCropPool();
       public static string ImagePath = "";
       public static int ThumbnailMaxSide = 0;
       private static int Height = 0;
       private static int Width = 0;

       public static Image LoadFromFile(string inPath, int minValue)
       {
           ThumbnailMaxSide = minValue;
           ImagePath = inPath;

           return Load();
       }
          /// <summary>
          ///获取路径中最大的照片
          /// </summary>
          /// <param name="inPath"></param>
          /// <param name="minValue"></param>
          /// <returns></returns>
       public static Image LoadMaxFormFolder(string inPath, int minValue)
       {
           string path = MaxImagePath(inPath);
           Image img=null;
           if (File.Exists(path))
           {
               img = LoadFromFile(path, minValue);
           }
           else
           {
               img = null;
           }
           return img;
       }
      /// <summary>
      /// 获取路径中 最大的图
      /// </summary>
      /// <param name="inFolder"></param>
      /// <returns></returns>
       public static string MaxImagePath(string inFolder)
       {
           string path = "";
          // IEImage ieImage = ieImagePool.CheckOut();
          using (IEImage ieImage = new IEImage())
          {
              try
              {
                 //;ieImage  
                  if (Directory.Exists(inFolder))
                  {
                      double aresPhoto = 0;

                      foreach (string filePath in Directory.GetFiles(inFolder, "*.*", SearchOption.AllDirectories))
                      {
                          ieImage.LoadImageInfo(filePath);
                          IOParams ioParams = ieImage.IOParams;
                          int vHeight = ioParams.OriginalHeight;
                          int vWidth = ioParams.OriginalWidth;
                          if (vHeight*vWidth > aresPhoto)
                          {
                              aresPhoto = vHeight*vWidth;
                              path = filePath;
                          }

                      }

                  }
                  else
                  {
                      return "";
                  }

              }
              catch (Exception)
              {


              }
              finally
              {
                  ieImage.Blank();
                  // ieImagePool.CheckIn(ieImage);
              }
          }
          return path;
       }

       static Image Load()
       {
           //Match match = urlRegex.Match(ImagePath);
           Bitmap bitmap = null;
          // IEImage ieImage = ieImagePool.CheckOut();
           using (IEImage ieImage = new IEImage())
           {



               ieImage.LoadImageInfo(ImagePath);

               IOParams ioParams = ieImage.IOParams;
               Height = ioParams.OriginalHeight;
               Width = ioParams.OriginalWidth;
               decimal sc = 0;
               if (Height > Width)
               {
                   sc = Height;
               }
               else
               {
                   sc = Width;
               }
               sc = sc/16;
               if (ioParams.OriginalWidth > sc || ioParams.OriginalHeight > sc)
               {
                   if (ioParams.OriginalWidth < ioParams.OriginalHeight)
                   {
                       ioParams.Height = (int) ThumbnailMaxSide;
                       ioParams.Width =
                           (int) ((decimal) ThumbnailMaxSide/ioParams.OriginalHeight*ioParams.OriginalWidth);
                   }
                   else
                   {
                       ioParams.Width = (int) ThumbnailMaxSide;
                       ioParams.Height =
                           (int) ((decimal) ThumbnailMaxSide/ioParams.OriginalWidth*ioParams.OriginalHeight);
                   }

                   ioParams.JPEG_UsedScale = 1;
                   ioParams.JPEG_Scale = IOParams.JPEGScale.AutoCalc;
                   ieImage.LoadImage(ImagePath);
               }

               Height = ioParams.OriginalHeight;
               Width = ioParams.OriginalWidth;
               int nWidth = ThumbnailMaxSide;
               int nHeight = (int) ((decimal) Height*nWidth/Width);
               ieImage.Resample(nWidth, nHeight, IEResampleFilter.BSpline);
               bitmap = ieImage.CreateBitmap();

               ieImage.Blank();
           }
           //ieImagePool.CheckIn(ieImage);

           return bitmap;
       }
       public static DiBImageInfo CreateDIBImage(string path)
       {
           DiBImageInfo rtnInfo = new DiBImageInfo(0, 0);
           IntPtr dibPtr = IntPtr.Zero;
           using (IEImage ieImage = new IEImage(path))
           {
               
               dibPtr = ieImage.CreateDIB(0, 0, ieImage.Width, ieImage.Height);
               rtnInfo = new DiBImageInfo(ieImage.Width, ieImage.Height);
               rtnInfo.Ptr = dibPtr;
               ieImage.Blank();
           }
           return rtnInfo;
       }

    }
}
